#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Aset
{
	ElemType data[100];
	int length;
}Aset;
void CreateSet(Aset* pa, int a[], int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		pa->data[i] = a[i];
	}
	pa->length = n;
}
void DispSet(Aset a)
{
	int i = 0;
	for (i = 0; i < a.length; i++)
	{
		printf("%d ", a.data[i]);
	}
	printf("\n");
}
void Add(Aset a, Aset b,Aset* psum)

{
	int i = 0;
	int j = 0;
	int k = 0;	
	psum->length = 0;
	while (i<a.length && j<b.length)
	{
		if ((a.data[i]) > (b.data[j]))
		{
			psum->data[k] = b.data[j];
			k++;
			j++;
		}
		else if ((a.data[i]) < (b.data[j]))
		{
			psum->data[k] = (a.data[i]);
			k++;
			i++;
		}
		else if((a.data[i]) == (b.data[j]))
		{
			psum->data[k] =(a.data[i]);
			k++;
			i++;
			j++;
		}
	}
	if (j == b.length)
	{
		while (i < a.length)
		{
			psum->data[k] = a.data[i];
			k++;
			i++;
		}
	}
	if (i == a.length)
	{
		while (j <b.length)
		{
			psum->data[k] = b.data[j];
			j++;
			k++;
		}
	}
	psum->length = k;
}
void Sub(Aset a, Aset b, Aset* psub)
{
	int i = 0;
	int j = 0;
	int k = 0;
	while (i<a.length&&j<b.length)
	{
		if (a.data[i] < b.data[j])
		{
			psub->data[k] = a.data[i];
			i++; 
			k++;
		}
		else if (a.data[i] > b.data[j])
		{
			j++;
		}
		else
		{
			j++;
			i++;
	
		}
	}
	if (j == b.length)
	{
		while (i < a.length)
		{
			psub->data[k] = a.data[i];
			i++;
			k++;
		
		}
	}
	psub->length = k;
}
void Inter(Aset a, Aset b, Aset* pinter)
{
	int i = 0;
	int j = 0;
	int k = 0;
	while (i < a.length&&j < b.length)
	{
		if (a.data[i] < b.data[j])
		{
			i++;
		}
		else if (a.data[i] > b.data[j])
		{
			j++;
		}
		else
		{
			pinter->data[k] = a.data[i];
			j++;
			i++;
			k++;

		}
	}
	pinter->length = k;
}
int main()
{
	Aset a1, a2;
	Aset sum, sub,pinter;
	int arr1[2] = { 9,10 };
	int arr2[3] = {  6, 8, 10 };
	CreateSet(&a1, arr1, 2);
	CreateSet(&a2, arr2, 3);
	
	DispSet(a1);
	DispSet(a2);
	Add(a1, a2, &sum);
	DispSet(sum);
	Sub(a1, a2, &sub);
	DispSet(sub);
	Inter(a1, a2, &pinter);
	DispSet(pinter);

}